package vip.xiaonuo.message.config;

import org.springframework.context.annotation.Bean;
import vip.xiaonuo.facedoor.modular.message.LocalMessageRepository;
import vip.xiaonuo.facedoor.modular.message.MessageSender;
import vip.xiaonuo.facedoor.modular.message.ReliableMessageCompensator;
import vip.xiaonuo.facedoor.modular.message.ReliableMessageSender;
import vip.xiaonuo.message.impl.JdbcTemplateBasedLocalMessageRepository;
import vip.xiaonuo.message.impl.LocalTableBasedReliableMessageCompensator;
import vip.xiaonuo.message.impl.LocalTableBasedReliableMessageSender;
import vip.xiaonuo.message.service.ReliableMessageSendService;

import javax.sql.DataSource;


public abstract class LocalTableMessageSenderSupper {

    @Bean
    public ReliableMessageSender reliableMessageSender(ReliableMessageSendService reliableMessageSendService){
        return new LocalTableBasedReliableMessageSender(reliableMessageSendService);
    }

    @Bean
    public ReliableMessageCompensator reliableMessageCompensator(ReliableMessageSendService reliableMessageSendService){
        return new LocalTableBasedReliableMessageCompensator(reliableMessageSendService);
    }

    @Bean
    public ReliableMessageSendService reliableMessageSendService(LocalMessageRepository localMessageRepository, MessageSender messageSender){
        return new ReliableMessageSendService(localMessageRepository, messageSender);
    }

    @Bean
    public LocalMessageRepository localMessageRepository(){
        return new JdbcTemplateBasedLocalMessageRepository(dataSource(), messageTable());
    }

    protected abstract DataSource dataSource();

    protected abstract String messageTable();

}
